<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\api\controller\ai;


use app\api\controller\BaseApiController;
use app\api\logic\ai\AiContentLogic;
use app\api\service\ai\AiContentService;
use app\api\validate\ai\AiContentValidate;
use app\common\model\ai\AiAssistant;


/**
 * openai
 * Class OpenAiController
 * @package app\api\controller\gpt
 */
class OpenAiController extends BaseApiController
{

    public array $notNeedLogin = ['chat'];

    public function initialize()
    {
        parent::initialize();
    }

    /*//
    public function hello()
    {
        try {
            $response = $this->client->chat()->create([
                'model'    => 'gpt-3.5-turbo',
                'messages' => [
                    ['role' => 'user', 'content' => '美国人的身高分布'],
                ],
            ]);
            dd($response->choices[0]->message->content);
            echo $response->choices[0]->message->content;
            $data = $response->toArray(); // ['object' => 'list', 'data' => [...]]
            return $this->success('操作成功', $data);
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function hello2()
    {
        // 创建一个新的对话并获取会话 ID
        $response = $this->client->chat()->createStreamed([
            'model'    => 'gpt-3.5-turbo',
            'messages' => [
                ['role' => 'user', 'content' => 'Hello!'],
            ],
        ]);

        // 从响应中获取会话 ID
        $data['session_id'] = $response->getStreamId(); // 获取新会话的 ID
        return $this->success('操作成功', $data);
    }

    // 获取可调用的模型
    public function getModelList()
    {
        $response = $this->client->models()->list();
        $response->object; // 'list'
        foreach ($response->data as $result) {
            $result->id; // 'gpt-3.5-turbo-instruct'
            $result->object; // 'model'
            // ...
        }
        $data = $response->toArray(); // ['object' => 'list', 'data' => [...]]
        return $this->success('操作成功', $data);
    }

    // 检索会话
    public function retrieveSession()
    {
        $response = $this->client->models()->retrieve('chatcmpl-95VsfFtpviPZ1XRSq1lkJJvttkOvR');
        $data     = $response->toArray(); // ['id' => 'gpt-3.5-turbo-instruct', ...]
        return $this->success('操作成功', $data);
    }*/

    // 聊天
    public function chat()
    {
        $params = (new AiContentValidate())->post()->goCheck('chat');
        AiContentLogic::chat($params);
        return $this->success('操作成功');
    }


}